﻿using LeadChina.NETFramework.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace LeadChina.AssemblyLine.Common.UDP
{
    public class InjectionUDP
    {
        private UDPTool udpTool;

        private IPEndPoint targetIPE;
        private byte[] frameHeader;

        public InjectionUDP(IPEndPoint localIPE, IPEndPoint _targetIPE)
        {
            udpTool = new UDPTool(localIPE);
            targetIPE = _targetIPE;
            frameHeader = new byte[] { 0x80/*ICF*/, 0x00/*RSV固定00*/, 0x02/*GCT固定02*/,
                0x00/*DNA目标网络号*/, Convert.ToByte(targetIPE.Address.ToString().Split('.')[3])/*DA1目标节点号*/,
                0x00/*DA2目标单元号*/, 0x00/*SNA源网络号*/, 0xC7/*SA1源节点号*/, 0x00/*SA2源单元号*/, 0x00/*SID服务号*/,
                0x01, 0x02/*0101表示读取，0102表示写入*/, };
        }

        /// <summary>
        /// 注液量设定
        /// </summary>
        /// <param name="value">注液量</param>
        /// <returns></returns>
        public bool SendInjectionVolumn(float value)
        {
            string sendValue = (value * 100).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 8 - valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }
            //R
            byte[] textBytes = { 0x82, 0x0D, 0xAC, 0x00, 0x00, 0x02, Convert.ToByte(sendValue.Substring(4, 2), 16), Convert.ToByte(sendValue.Substring(6, 2), 16), Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };
            //L
            byte[] textBytes2 = { 0x82, 0x0E, 0x10, 0x00, 0x00, 0x02, Convert.ToByte(sendValue.Substring(4, 2), 16), Convert.ToByte(sendValue.Substring(6, 2), 16), Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };
            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeader.Length + textBytes.Length];

                frameHeader.CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeader.Length);

                udpTool.Send(sendBytes, targetIPE);

                byte[] recvBytes = udpTool.Receive();

                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    LogHelper.WriteLog($"注液量下发成功");
                    return true;
                }
                else
                {
                    LogHelper.WriteLog($"注液量下发失败");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"注液量下发异常");
                return false;
            }
        }

        /// <summary>
        /// R头注液量设定
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SendInjectionRVolumn(float value)
        {

            string sendValue =  (Math.Round( value,2) * 100).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 8 - valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }
            //R
            byte[] textBytes = { 0x82, 0x0D, 0xAC, 0x00, 0x00, 0x02, Convert.ToByte(sendValue.Substring(4, 2), 16), Convert.ToByte(sendValue.Substring(6, 2), 16), Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };

            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeader.Length + textBytes.Length];
                frameHeader.CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeader.Length);
                udpTool.Send(sendBytes, targetIPE);
                Thread.Sleep(1000);
                byte[] recvBytes = udpTool.Receive();
                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    LogHelper.WriteLog($"注液量R下发成功");
                    return true;
                }
                else
                {
                    LogHelper.WriteLog($"注液量R下发失败");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"注液量下发异常:{ex}");
                return false;
            }
        }
        /// <summary>
        /// L头注液量设定
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool SendInjectionLVolumn(float value)
        {
            string sendValue = (Math.Round(value, 2) * 100).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 8 - valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }
            //L
            byte[] textBytes = { 0x82, 0x0E, 0x10, 0x00, 0x00, 0x02, Convert.ToByte(sendValue.Substring(4, 2), 16), Convert.ToByte(sendValue.Substring(6, 2), 16), Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };
            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeader.Length + textBytes.Length];

                frameHeader.CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeader.Length);

                udpTool.Send(sendBytes, targetIPE);

                byte[] recvBytes = udpTool.Receive();

                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    LogHelper.WriteLog($"注液量L下发成功");
                    return true;
                }
                else
                {
                    LogHelper.WriteLog($"注液量L下发失败");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"注液量L下发异常");
                return false;
            }
        }

        /// <summary>
        /// 补偿量设定
        /// </summary>
        /// <param name="value">补偿量</param>
        /// <returns></returns>
        public bool SendCompensationAmount(float value)
        {
            string sendValue = (Math.Round(value, 2) * 100 + 5000).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 4 - valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }

            byte[] textBytes = { 0x82, 0x10, 0x04, 0x00, 0x00, 0x01, Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };

            byte[] textBytes2 = { 0x82, 0x10, 0x68, 0x00, 0x00, 0x01, Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };
            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeader.Length + textBytes.Length];

                frameHeader.CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeader.Length);

                udpTool.Send(sendBytes, targetIPE);

                byte[] recvBytes = udpTool.Receive();

                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    LogHelper.WriteLog($"补液量下发成功");
                    return true;
                }
                else
                {
                    LogHelper.WriteLog($"补液量下发失败");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"补液量下发异常");
                return false;
            }
        }

        /// <summary>
        /// R补偿量设定
        /// </summary>
        /// <param name="value">补偿量</param>
        /// <returns></returns>
        public bool SendCompensationRAmount(float value)
        {
            string sendValue = (Math.Round(value, 2) * 100 + 5000).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 4 - valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }

            byte[] textBytes = { 0x82, 0x10, 0x04, 0x00, 0x00, 0x01, Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };

            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeader.Length + textBytes.Length];

                frameHeader.CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeader.Length);

                udpTool.Send(sendBytes, targetIPE);

                byte[] recvBytes = udpTool.Receive();

                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    LogHelper.WriteLog($"补液量R下发成功");
                    return true;
                }
                else
                {
                    LogHelper.WriteLog($"补液量R下发失败");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"补液量R下发异常");
                return false;
            }
        }
        /// <summary>
        /// L补偿量设定
        /// </summary>
        /// <param name="value">补偿量</param>
        /// <returns></returns>
        public bool SendCompensationLAmount(float value)
        {
            string sendValue = (value * 100 + 5000).ToString();
            int valueLength = sendValue.Length;
            for (int i = 0; i < 4 - valueLength; i++)
            {
                sendValue = '0' + sendValue;
            }
            
            byte[] textBytes = { 0x82, 0x10, 0x68, 0x00, 0x00, 0x01, Convert.ToByte(sendValue.Substring(0, 2), 16), Convert.ToByte(sendValue.Substring(2, 2), 16) };
            try
            {
                //发送的字节
                byte[] sendBytes = new byte[frameHeader.Length + textBytes.Length];

                frameHeader.CopyTo(sendBytes, 0);
                textBytes.CopyTo(sendBytes, frameHeader.Length);

                udpTool.Send(sendBytes, targetIPE);

                byte[] recvBytes = udpTool.Receive();

                if (recvBytes[12] == 0x00 && recvBytes[13] == 0x00)
                {
                    LogHelper.WriteLog($"补液量L下发成功");
                    return true;
                }
                else
                {
                    LogHelper.WriteLog($"补液量L下发失败");
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog($"补液量L下发异常");
                return false;
            }
        }

    }
}
